#include "TimeBlock.h"    

TimeBlock::TimeBlock(vector<AAM::Imagens*>::iterator _begin, vector<AAM::Imagens*>::iterator _end) {
	begin = _begin;
	end = _end;
	int min = 60;
	int hora = 60 * min;
	int dia = 24 * hora;

	double diffTime = 0;
	vector<AAM::Imagens*>::iterator it = _begin;
	while (it != (end - 1)) {
		//    for(int a=1;a<allImages.size();a++){
		AAM::Imagens *im1, *im2;
		im2 = (*(it + 1));
		im1 = (*it);
		double diff = -log10(im2->getTime() - im1->getTime());
		if (diff < diffTime)
			diffTime = diff;

		it++;
	}


	//> 1 dia
	if (diffTime < -log10(dia + 1))
		divisorias.push_back(-log10(dia + 1));
	//> 8 horas
	if (diffTime < -log10(8 * hora + 1))
		divisorias.push_back(-log10(8 * hora + 1));
	//> 4 horas
	if (diffTime < -log10(4 * hora + 1))
		divisorias.push_back(-log10(4 * hora + 1));
	//> 1 hora
	if (diffTime < -log10(hora + 1))
		divisorias.push_back(-log10(hora + 1));
	//> 30 min
	if (diffTime < -log10(30 * min + 1))
		divisorias.push_back(-log10(30 * min + 1));
	//> 1 min
	if (diffTime < -log10(5*min + 1))
		divisorias.push_back(-log10(5*min + 1));
	if (divisorias.size() != 0)
		encontrarDivisorias(begin, end, divisorias, 0);
}

void TimeBlock::encontrarDivisorias(vector<AAM::Imagens*>::iterator begin, vector<AAM::Imagens*>::iterator end, vector<double> divisorias, int level) {

	vector<pair<vector<AAM::Imagens*>::iterator, vector<AAM::Imagens*>::iterator> > pares;

	vector<AAM::Imagens*>::iterator it = begin + 1;
	vector<AAM::Imagens*>::iterator lastDivisor = begin;
	while (it != end) {
		double diff = -log10((*it)->getTime()-(*(it - 1))->getTime() + 1);
		if (diff < divisorias[level]) {
			//
			pair<vector<AAM::Imagens*>::iterator, vector<AAM::Imagens*>::iterator> p(lastDivisor, it);
			pares.push_back(p);
			lastDivisor = it;
		}
		it++;
	}
	if (lastDivisor != end) {
		pair<vector<AAM::Imagens*>::iterator, vector<AAM::Imagens*>::iterator> p(lastDivisor, end);
		pares.push_back(p);
	}
	//        if(pares.size()==0 || level>divisorias.size()-1){
	//            //Leafs
	////                ofstream out("organizacao.txt",ios_base::app);
	////
	////                string indent("");
	////                for(int a=0;a<level;a++){
	////                    indent.append("\t");
	////                }
	//                vector<AAM::Imagens*>::iterator it = begin;
	//                while(it!=end){
	////                    out << endl << indent << (*it)->getFilename() << " " << (*it)->getTime();
	//                    it++;
	//                }
	////                out.close();
	//        }else{
	if (pares.size() != 0) {

		for (int m = 0; m < pares.size(); m++) {
			//                    ofstream out("organizacao.txt",ios_base::app);
			//                    string indent("");
			//                    for(int a=0;a<level;a++){
			//                        indent.append("\t");
			//                    }
			//                    out << endl << indent << level << "." << m;
			//                    out.close();
			//                    encontrarDivisorias(pares[m].first,pares[m].second,divisorias,level+1);
			if (pares[m].first != pares[m].second) {
				TimeBlock *tb = new TimeBlock(pares[m].first, pares[m].second);
				addTimeBlock(tb);
			}
		}

	}
}

void TimeBlock::addTimeBlock(TimeBlock *block) {
	tb.push_back(block);
}

int TimeBlock::getNumberOfImagesInThisBlock(){
	if(tb.size()==0){
		return end-begin;
	}else{
		int totalImages = 0;
		for(int a=0;a<tb.size();a++){
			totalImages += tb[a]->getNumberOfImagesInThisBlock();
			
		}
		return totalImages;
	}
}
vector<TimeBlock*> TimeBlock::getSubBlocks() {
	return tb;
}

vector<AAM::Imagens*>::iterator TimeBlock::getBegin() {
	return begin;
}

vector<AAM::Imagens*>::iterator TimeBlock::getEnd() {
	return end;
}

int TimeBlock::getNumberOfNodes() {
	int numNodes = 0;
	if (tb.size() == 0)
		numNodes = 1;
	else {
		for (int a = 0; a < tb.size(); a++) {
			numNodes += tb[a]->getNumberOfNodes();
		}

	}
	return numNodes;
	
}
int TimeBlock::getNumberOfSubBlocks(){
	return tb.size();
}

bool TimeBlock::isLeaf() {
	if (tb.size() == 0)
		return true;
	else
		return false;
}

void TimeBlock::reduceToNNodes(int n) {
	//        if(!isLeaf()){
	//            vector<int> amount;
	//            int totalNodes = 0;
	//            for(int a=0;a<tb.size();a++){
	//                int nnodes = tb[a]->getNumberOfNodes();
	//                amount.push_back(nnodes);
	//                totalNodes+=nnodes;
	//            }
	//            
	//        }
	//        while(getNumberOfNodes() > n){
	//            
	//        }
}

